package com.thread;

import java.util.concurrent.Callable;

/**
 * Created by buguniao on 16/4/16.
 * futre 是怎么做到，一提交一个任务就可以马上返回一个futre，然后在后续的时间里，可以去调用他的get方法来得到这个结果？
 */
public class FutureTaskTest {
    static class MyTask implements Runnable{
        private String result ="";
        public void run(){
            System.out.println("start ....");
            try {
                Thread.sleep(2000);
                synchronized (this){
                    System.out.println("get result lock,slepp 10s----1");
                    Thread.sleep(10000);
                    result="xxxxxx";
                    this.notify();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("end ....");


        }
        public String get() throws InterruptedException {
            synchronized (this){
                System.out.println("get result lock---2");
                if("".equals(result)){
                    this.wait();
                }
                return result;
            }

        }
    }

    public static void main(String[] args) throws InterruptedException {
        MyTask task = new MyTask();
        Thread thread = new Thread(task);
        thread.start();
        String rs = task.get();
        System.out.println(rs);
    }
}
